package com.vken.blog.service.impl;

import java.util.List;

import com.vken.blog.dao.CommentDao;
import com.vken.blog.dao.EntryDao;
import com.vken.blog.model.Comment;
import com.vken.blog.model.Entry;
import com.vken.blog.service.CommentManager;
import com.vken.blog.util.Pagination;

public class CommentManagerImpl implements CommentManager {
	private CommentDao commentDao;
	private EntryDao entryDao;

	public void deleteComments(Integer[] commentId) {
		Comment comment = null;
		for (int i = 0; i < commentId.length; ++i) {
			comment = commentDao.findById(commentId[i]);
			if (comment != null) {
				commentDao.delete(commentDao.findById(commentId[i]));
			}
		}
		entryDao.updateCommentHit(comment.getEntry().getId());
	}

	public Pagination getListByBlogId(Integer blogId, Integer status, int pageSize, int pageNumber) {
		return commentDao.getPaginationByBlogId(blogId, status, pageSize, pageNumber);
	}

	public List<Comment> getListByEntryId(Integer entryId) {
		return commentDao.getCommentsByEntryId(entryId);
	}

	public void insert(Comment comment, Integer entryId) {
		Entry entry = entryDao.findById(entryId);
		comment.setEntry(entry);
		commentDao.save(comment);
		entryDao.updateCommentHit(entryId);
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}

	public void updateComments(Integer[] commentId) {
		Comment comment = null;
		for (int i = 0; i < commentId.length; ++i) {
			comment = commentDao.findById(commentId[i]);
			if (comment != null) {
				comment.setStatus(Math.abs(comment.getStatus() - 1));
				commentDao.update(comment);
			}
		}
		entryDao.updateCommentHit(comment.getEntry().getId());
	}

}
